/**
 * 部门管理初始化
 */
var PaymentRecord = {
    id: "PaymentRecordTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
PaymentRecord.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},

        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {
            title: 'reservationsId',
            field: 'reservationsId',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: '50px'
        },
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,},
        {title: '客户电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true,},
        {
            title: '客户性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                } else {
                    return "未知"
                }
            }
        },
        {title: '客户年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true,},
        {title: '客户状态旧', field: 'customerStatus', align: 'center', valign: 'middle',sortable: true,},
        {title: '客户状态', field: 'xcustomerStatus', align: 'center', valign: 'middle', sortable: true,},
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true,},
        // {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true,},
        // {title: '化名', field: 'pseudonym', align: 'center', valign: 'middle', sortable: true,},
        {title: '结账金额', field: 'actualAccountBalance', align: 'center', valign: 'middle', sortable: true,},
        {title: '结账方式', field: 'checkoutMethod', align: 'center', valign: 'middle', sortable: true,},
        {
            title: '结账时间', field: 'rechargeTime', align: 'center', valign: 'middle', sortable: true, width: '12%',
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true},
    ]
    return columns;
};

PaymentRecord.close = function () {
    // parent.layer.close(window.parent.Customer.layerIndex);
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}
PaymentRecord.updateCheckoutMethod = function () {
    if($("#checkoutMethod").val()==null||$("#checkoutMethod").val()==""){
        alert("请选择收费方式！");
        return;
    }
    $.ajax({
        url: Feng.ctxPath + "/paymentRecord/updateCheckoutMethod",
        type: "post",
        data: {
            "id": $('#id').val(),
            "checkoutMethod": $("#checkoutMethod").val()
        },
        success: function (data) {
            Feng.info("收费方式修改成功！");
            window.parent.PaymentRecord.table.refresh();
            PaymentRecord.close();
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
}
/**
 * 检查是否选中
 */
PaymentRecord.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        PaymentRecord.seItem = selected[0];
        return true;
    }
};

/**
 * 查询预约客户列表
 */
PaymentRecord.search = function () {
    PaymentRecord.table.refresh({query: PaymentRecord.formParams()});
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/paymentRecord/sumBorrowAmount',
        data: PaymentRecord.formParams(),
        dataType: "json",
        success: function (data) {
            $('#sumMoney').html(data.borrowAmount_sum);
        }
    });
};

PaymentRecord.resetSearch = function () {
    $("#startTime").val("");
    $("#endTime").val("");
    $("#checkoutMethod").val("");
    $("#fieldTc").val("");
    $("#doctor").val("");
    $("#xcustomerStatus").val("");
    $("#reach").val("");
    document.getElementById("startTime").value = y + "-" + m + "-" + d + " " + "16:00:00";
    document.getElementById("endTime").value = Y + "-" + M + "-" + D + " " + "16:00:00";
    PaymentRecord.search();
};

PaymentRecord.toUpdate = function () {
    if (this.check()) {
        var customerName=PaymentRecord.seItem.customerName;
        var id=PaymentRecord.seItem.id;
        var ci=customerName+","+id;
        var index = layer.open({
            type: 2,
            title: '成交操作',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/paymentRecord/toUpdate/' + ci
        });
        this.layerIndex = index;
    }
};

var parseParam = function (param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + param;
    } else {
        $.each(param, function (i) {
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};

PaymentRecord.downLoad = function () {
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/paymentRecord/paymentExport?' + param;
}

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
PaymentRecord.formParams = function () {
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();

    var checkoutMethod = $('#checkoutMethod').val();
    var str1 = "";
    if (checkoutMethod != null && checkoutMethod != "") {
        for (var i = 0; i < checkoutMethod.length; i++) {
            if (i == (checkoutMethod.length - 1)) {
                str1 += checkoutMethod[i];
            } else {
                str1 += checkoutMethod[i] + ",";
            }
        }
    }
    queryData['checkoutMethod'] = str1;

    var fieldTc = $('#fieldTc').val();
    var str2= "";
    if (fieldTc != null && fieldTc != "") {
        for (var i = 0; i < fieldTc.length; i++) {
            if (i == (fieldTc.length - 1)) {
                str2 += fieldTc[i];
            } else {
                str2 += fieldTc[i] + ",";
            }
        }
    }
    queryData['fieldTc'] = str2;

    queryData['doctor'] = $("#doctor").val();
    queryData['reach'] = $("#reach").val();

    var xcustomerStatus = $('#xcustomerStatus').val();
    var str3= "";
    if (xcustomerStatus != null && xcustomerStatus != "") {
        for (var i = 0; i < xcustomerStatus.length; i++) {
            if (i == (xcustomerStatus.length - 1)) {
                str3 += xcustomerStatus[i];
            } else {
                str3 += xcustomerStatus[i] + ",";
            }
        }
    }
    queryData['xcustomerStatus'] = str3;
    return queryData;
};

var myDate = new Date();
var Y = myDate.getFullYear();
var M = myDate.getMonth() + 1;
var D = myDate.getDate();
if(M < 10){
    M = "0" + M
}
if(D < 10){
    D = "0" + D
}
var date = new Date();
date.setDate(date.getDate()-1);
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
if(m < 10){
    m = "0" + m
}
if(d < 10){
    d = "0" + d
}

$(function () {
    $.post(Feng.ctxPath + "/paymentRecord/getList", function (data) {
        // var txt = '<option value="">--请选择--</option>';
        // for (var i = 0; i < data.length; i++) {
        //     txt += '<option value="' + data[i].checkout_method + '">' + data[i].checkout_method + '</option>';
        // }
        // document.getElementById('checkoutMethod').innerHTML = txt;
        // //状态单选
        // var seletcheckoutMethod = $('#checkoutMethodValue').val();
        // if (seletcheckoutMethod != "") {
        //     $("#checkoutMethod option[value=" + seletcheckoutMethod + "]").attr("selected", "selected");
        // }
        for (var i = 0; i < data.length; i++) {
            $('#checkoutMethod').append("<option value=" + data[i].checkout_method + ">" + data[i].checkout_method + "</option>");
        }
        $('#checkoutMethod').selectpicker('refresh');
        $('#checkoutMethod').selectpicker('render');
    }, "json");
    $.post(Feng.ctxPath + "/tc/getList", function (data) {
        // var txt = '<option value="">--请选择TC--</option>';
        // for (var i = 0; i < data.length; i++) {
        //     txt += '<option value="' + data[i].tcName + '">' + data[i].tcName + '</option>';
        // }
        // document.getElementById('fieldTc').innerHTML = txt;
        // //状态单选
        // var seletefieldTc = $('#fieldTcValue').val();
        // if (seletefieldTc != "") {
        //     $("#fieldTc option[value=" + seletefieldTc + "]").attr("selected", "selected");
        // }
        for (var i = 0; i < data.length; i++) {
            $('#fieldTc').append("<option value=" + data[i].tcName + ">" + data[i].tcName + "</option>");
        }
        $('#fieldTc').selectpicker('refresh');
        $('#fieldTc').selectpicker('render');
    }, "json");
    document.getElementById("startTime").value = y + "-" + m + "-" + d + " " + "16:00:00";
    document.getElementById("endTime").value = Y + "-" + M + "-" + D + " " + "16:00:00";
    var defaultColunms = PaymentRecord.initColumn();
    var table = new BSTable(PaymentRecord.id, "/paymentRecord/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(PaymentRecord.formParams());
    PaymentRecord.table = table.init();
    PaymentRecord.search();

    var menuName = $("#menuName").val();
    $.ajax({
        url: Feng.ctxPath + "/reservation/reservation_initialization",
        type: "post",
        data: {
            "menuName": menuName
        },
        success: function (data) {
            if(data != ""){
                var item = data.listx1.split("，");
                for(var i = 0 ; i < item.length ; i++){
                    $('#PaymentRecordTable').bootstrapTable('hideColumn', item[i]);
                }
            }
        }
    });

    //采用定时器监听 时间内容是否变化
    var startArrivalTimeTwo = $("#startTime").val();
    var endArrivalTimeTwo = $("#endTime").val();

    setInterval(
        function () {//定时执行方法内容
            if ($("#startTime").val() != startArrivalTimeTwo || $("#endTime").val() != endArrivalTimeTwo) {//如果时间发生变化，就触发查询事件
                PaymentRecord.search()
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                startArrivalTimeTwo = $("#startTime").val();
                endArrivalTimeTwo = $("#endTime").val();
            }
        }, 500);

});
